#include <linux/version.h>
#include <linux/input.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

#include "spi.h"


//#include <sys/ioctl.h>
//#include <linux/ioctl.h>
#include <errno.h>
#include <termios.h>


#define SPORT_UART_DEVICE "/dev/ttySS1"

static int spi_fd;
static int uart_fd;

//unsigned char data [20] = {0};//{0xe0,0x03,0x06,0x01,0xea};//{0xe0,0x06,0x06,0x01,0x12,0xea,};
unsigned char send_data_buf [5] = {0};
extern unsigned char read_info_data[20];


void init_sports_uart (int uart_fd)
{
struct termios options;			
//fcntl(uart_fd, F_SETFL, 0);	
// set default setting	
tcgetattr(uart_fd, &options);
//	cfsetispeed(&options, B4800);// B115200 bps	
cfsetospeed(&options, B38400);	// B115200	
options.c_cflag &= ~PARENB;	
options.c_cflag &= ~CSTOPB;	
options.c_cflag &= ~CSIZE;	
options.c_cflag |= CS8;				// n, 8, 1

// enable checking and stripping of the parity bit
//	options.c_iflag |= (INPCK | ISTRIP);	
// don't do 'Map CR to NL'	options.c_iflag &= ~ICRNL;	
// disable software flow control	
options.c_iflag &= ~(IXON | IXOFF | IXANY);	
// process hardware flow control, set raw input, 3 second timeout	
options.c_cflag |= CRTSCTS;	
options.c_cflag |= (CLOCAL | CREAD);	
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);	
options.c_oflag &= ~OPOST;


options.c_cc[VMIN] = 0;	
options.c_cc[VTIME] = 30;	
tcsetattr(uart_fd, TCSANOW, &options);	
// set default setting - 115200, N, 8, 1	tcgetattr(uart_fd, &options);	
printf("dev_GpsInit: ok\n");
}


int  dev_UartInit(void)
{	
	
	uart_fd = open(SPORT_UART_DEVICE, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);// | O_NOCTTY | O_NDELAY);	
	if (uart_fd == -1) {		
		printf("%s open error %d\n", SPORT_UART_DEVICE, errno);		
		exit(1);	
		}		
	else
		printf("open success %s \n", SPORT_UART_DEVICE);  
	init_sports_uart (uart_fd);	
}

int UartDataChk (void)
{
	unsigned char len,i;
	unsigned char chksum=0;	
		
	
	if ((read_info_data[0] != MODULE_TEST_FLAG1) && (read_info_data[1] != MODULE_TEST_FLAG2))
		return -1;
	
	switch ( read_info_data[2])
	{
		case 	MODULE_TEST_INPUT_MOUSE:
			len = 6;
			break;
		case	MODULE_TEST_INPUT_KEYBOARD:
			break;
	}
	
	for (i=0; i<len; i++)
	{
			chksum =(unsigned char)(chksum +read_info_data[i]);		
	}
	if ( chksum != read_info_data [len])
	{
		return 0;//return -1;
	}
	else
		return 0;
		
		
}


int  dev_UartRead(unsigned char *buf)
{
	int length,i=0,rc=0;	
	unsigned char *p=buf;
	length = 10;
	
	
	rc = read(uart_fd,p,10);
	if (rc >0)
	{
		printf("read spi data : %d",rc);
		if (rc<7)
		{
			i = 7-rc;
			do{
				rc = read(uart_fd,p,i);
				}				
				while(rc>0);
				printf ("read lose,rc is %d\n",rc);
		}
		
		for (i=0; i<10; i++)
			printf(" %x,",buf[i]);
		printf("\n");
		return (UartDataChk());
	}
	return -1;
	
}


int  dev_SPIRead(unsigned char *buf)
{
	int length,i,rc=0;	
	unsigned char *p=buf;
	length = 10;
	
	
	rc = read(uart_fd,p,10);
/*	rc = read(uart_fd,p,1);
	if (( *p!= FLAG) || (rc <=0))
		return -1;		
		
	p++;
	rc = read(uart_fd,p,1);
	if (( *p < 0) || (rc <=0))
		return -1;
	else 
		length = *p;
	p++;
	
	for (i=0; i<length; i++)
	{		
		rc = read(uart_fd, p,1 );	//rc = read(uart_fd, p,length );
		if (rc ==-1)
			{
				i--;				
			}
		else
			p++;
		
	}	
	printf("rc :%d %x,%x,%x,%x,%x,%x,%x,%x,%x,%x*********\n",\
				rc,buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7],buf[8],buf[9]);
	*/			
	return rc;

   
	
}

int  dev_SPIWrite(unsigned char *buf,int len)
{
	int return_len;
	int i;
	
	return_len = write(uart_fd, buf,len);
	
	
	for (i=0; i<buf[1]+2; i++)
			printf(" %x,",buf[i]);
	printf("\n");
	
	return 0;
}


 int send_data_out (unsigned char *buf, int num)
{
	int i;
	unsigned char sum = 0;
	for (i = 0; i<num-1; i++)
		{		
		sum += *(buf+i);	
		}
	*(buf+(num-1)) =  sum;	
	
	
	if (dev_SPIWrite(buf,num)==0)
			return 0;
	return -1;
	
 	}

